﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AutumnEngine
{
    public class MainMap : Scene
    {
        public MainMap()
        {
            //Load the textures we need.
            //The texturemanager cannot be removed, we don't need to check if it exists.
            Engine.Modules.TextureManager.AddTexture("ManTexture", "TestingTexture");
            Engine.Modules.TextureManager.AddTexture("Platform", "Platform");
            Engine.Modules.SoundManager.AddSound("Bop", "Bop");
            
            //We'll add our fonts now.
            Engine.Modules.FontManager.AddFont("MainFont", "Fonts/Main");
            LabelNode l = new LabelNode("MainFont");
            l.Text = "Press 'D' to load.";
            l.Color = Color.DarkGray;
#if ZUNE
            l.Text = "3 Taps To Load.\n2 Taps For Sound";
            l.Scale = 0.45f;
#endif
            l.Position = new Vector2(20, 230);
            this.AddNode("Temp", l);

            //Set to true to load a test scene for saving.
            if(false)
            {
            LabelNode label = new LabelNode("MainFont");

#if ZUNE
            label.Text = "Autumn Engine v0.4 Stable Branch";
            label.Rotation = MathHelper.ToRadians(90);
            label.Scale = 0.45f;
            label.Origin = new Vector2(0, Engine.Modules.FontManager.MeasureString(label.Text, "MainFont").Y / 2);
#else
            label.Text = "Autumn\nEngine\nv0.4-Stable\n";            
#endif

            label.Position = new Vector2(15, 30);
            label.Color = Color.White;
            this.AddNode("Label", label);

            //Create the physic node.
            PhysicsSpriteNode phys = new PhysicsSpriteNode("ManTexture",BodyType.Automatic, 64f, 128f, 10f);
            phys.Color = Color.Green;
            this.AddNode("Physic", phys);

            //We'll make a platform for the physic node.
            PhysicsSpriteNode platform = new PhysicsSpriteNode("Platform", BodyType.Rectangular, 1280, 40f, 10f);
            platform.Position = new Vector2((1280 - 600), 480 - 20);
            platform.Static = true;
            this.AddNode("Platform", platform);

            }           
          
        }

        public override void Update()
        {
            if (Engine.Modules.Contains("Engine.MouseInput"))
            {
                if (this.Nodes.ContainsKey("Physic"))
                    if (Engine.Modules.MouseInput.IsLeftButtonDown)
                    {
                        this.Nodes["Physic"].Position = Engine.Modules.MouseInput.Position;                        
                            Engine.Modules.SoundManager.PlaySound("Bop", false);
                        
                    }
            }
            if (Engine.Modules.Contains("Engine.KeyboardInput"))
            {
                if (Engine.Modules.KeyboardInput.IsKeyTriggered(Microsoft.Xna.Framework.Input.Keys.S))
                {
                    this.Save("Content/TestScene.scn");
                }
                if (Engine.Modules.KeyboardInput.IsKeyTriggered(Microsoft.Xna.Framework.Input.Keys.D))
                {
                    this.Load("Content/TestScene.scn");
                }
            }

            if (Engine.Modules.Contains("Engine.TouchscreenInput"))
            {
                if (Engine.Modules.TouchscreenInput.DoesTouchExist(2))
                {
                    this.Load("Content/TestScene.scn");
                    return;
                }
                if (Engine.Modules.TouchscreenInput.DoesTouchExist(1))
                {
                    Engine.Modules.SoundManager.PlaySound("Bop", false);
                    return;
                }
                if (Engine.Modules.TouchscreenInput.DoesTouchExist(0))
                    if (this.Nodes.ContainsKey("Physic"))
                        this.Nodes["Physic"].Position = Engine.Modules.TouchscreenInput.CurrentTouches[0].Positon;
            }
            base.Update();
        }
    }
}
